package com.rcd2.service.impl;

import com.rcd2.mapper.BrandMapper;
import com.rcd2.pojo.Brand;
import com.rcd2.pojo.PageBean;
import com.rcd2.service.BrandService;
import com.rcd2.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * ClassName:BrandServiceImpl
 * Package:com.rcd2.service.impl
 * ProjectName:servlet
 * Description:
 *
 * @Date:2022/2/9 16:34
 * @Author:Mrhan
 */
public class BrandServiceImpl implements BrandService {
    //1,创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    @Override
    public List<Brand> selectAll() {
        /*
         * 2,获取SqlSession对象
         * 3,获取BrandMapper对象
         * 4,调用数据库操作的方法
         * 5,释放session资源
         * */
        SqlSession session = factory.openSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);
        List<Brand> brands = mapper.selectAll();
        session.close();

        return brands;
    }

    @Override
    public void add(Brand brand) {
        SqlSession session = factory.openSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);
        mapper.add(brand);
        session.commit();
        session.close();
    }

    @Override
    public void deleteByIds(int[] ids) {
        SqlSession session = factory.openSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);
        mapper.deleteByIds(ids);
        session.commit();
        session.close();
    }

    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        SqlSession session = factory.openSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);

        /**
         * currentPage是0,会查询失败,必须从1开始查询*/
        if (currentPage <= 0) {
            currentPage = 1;
        }
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        //计算查询条目数
        int size = pageSize;

        //返回给前端的列表数据和总条目数
        List<Brand> rows = mapper.selectByPage(begin, size);
        int totalCount = mapper.selectTotalCount();

        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        session.close();
        return pageBean;
    }

    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        SqlSession session = factory.openSession();
        BrandMapper mapper = session.getMapper(BrandMapper.class);

        /**
         * currentPage是0,会查询失败,必须从1开始查询*/
        if (currentPage <= 0) {
            currentPage = 1;
        }
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;
        //计算查询条目数
        int size = pageSize;

        String brand_name = brand.getBrand_name();
        if (brand_name!=null && brand_name.length()>0) {
            brand.setBrand_name("%"+brand_name+"%");
        }

        String company_name = brand.getCompany_name();
        if (company_name!=null && company_name.length()>0) {
            brand.setCompany_name("%"+company_name+"%");
        }

        //返回给前端的列表数据和总条目数
        List<Brand> rows = mapper.selectByPageAndCondition(begin, size, brand);
        int totalCount = mapper.selectTotalCountByCondition(brand);

        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        session.close();
        return pageBean;
    }

}
